<?php
/**
 * Zend Framework application
 *
 * @category     Application
 * @package      Admin
 * @subpackage   ViewHelper
 * @author       Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright    Copyright (c) 2010 Krzysztof Kardasz
 */

/**
 * Adres url elementu
 *
 * @category     Application
 * @package      Admin
 * @subpackage   ViewHelper
 * @author       Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright    Copyright (c) 2010 Krzysztof Kardasz
 * @version 	 1.0
 */
class Hesperos_View_Helper_ContentUrl extends Zend_View_Helper_Abstract
{
    /**
     * Tabela
     *
     * @var Application_Model_Gateway_Table_Content
     */
    private static $_table = null;

    /**
     * Zwraca instancję obiektu
     *
     * @return Application_Model_Gateway_Table_Content
     */
    public function getTable ()
    {
        if(self::$_table === null) {
            self::$_table = new Application_Model_Gateway_Table_Content ();
        }
        return self::$_table;
    }

    /**
     * Zwraca link do węzła w określonym języku
     *
     * @param  string $language język
     * @param  integer $nodeId id węzła
     * @return string
     */
    public function contentUrl ($language, $nodeId)
    {
        $nodeId = (int)$nodeId;
        if($nodeId) {
            $cache = Hesperos_Cache_Manager::getCache('page');
            $hash  = 'url' . sha1($language) . sha1($nodeId);
            if ($cache && ($url = $cache->load($hash)) !== false ) {
                return $url;
            }
            $table = $this->getTable();
            $translatedNode = $table->fetchRow(
                $table->select(
                    $table->getTableName(),
                    array('language', 'url','url_custom', 'type')
                )
                ->where('id = ?', $nodeId)
                ->where('language = ?', $language)
            );
            if($translatedNode) {
                if($translatedNode->type != Application_Model_Domain_Content::TYPE_PAGE) {
                    $parentNode = $table->fetchRow(
                        $table->select(
                            $table->getTableName(),
                            array('language', 'url','url_custom', 'type')
                        )
                        ->where('content_type = ?', (int)$translatedNode->type)
                        ->where('type = ?', Application_Model_Domain_Content::TYPE_PAGE)
                        ->where('language = ?', $language)
                        ->where('status = 3')
                    );
                    if($parentNode) {
                        if(!empty($parentNode->url_custom)) {
                            $url = $parentNode->url_custom;
                        } else {
                            $url = implode('/', array($parentNode->language, $parentNode->url));
                        }
                        $url .= '__' . $nodeId;
                        $url = $this->view->baseUrl ($url);
                    }
                } else {
                    if(!empty($translatedNode->url_custom)) {
                        $url = $translatedNode->url_custom;
                    } else {
                        $url = implode('/', array($translatedNode->language, $translatedNode->url));
                    }
                    $url = $this->view->baseUrl ($url);
                }
            }
            $cache->save($url, $hash);
        } else {
            $url = $this->view->baseUrl($language);
        }
        return $url;
    }
}